package cn.iocoder.yudao.module.famous.doctor.service.certificate;

import cn.hutool.core.util.IdcardUtil;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.famous.doctor.controller.admin.certificate.vo.*;
import cn.iocoder.yudao.module.famous.doctor.dal.dataobject.certificate.FamCertificateDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.famous.doctor.convert.certificate.FamCertificateConvert;
import cn.iocoder.yudao.module.famous.doctor.dal.mysql.certificate.FamCertificateMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
import static cn.iocoder.yudao.module.famous.doctor.enums.ErrorCodeConstants.*;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.CREATE_USER_CERTIFICATE_EXISTS;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.TREATMENT_USER_NOT_EXISTS_IDENTITYCARD;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 互联网备案 Service 实现类
 *
 * @author Test1
 */
@Service
@Validated
public class FamCertificateServiceImpl implements FamCertificateService {

    @Resource
    private FamCertificateMapper famCertificateMapper;

    @Override
    public Long createFamCertificate(FamCertificateCreateReqVO createReqVO) {
        // 插入
        FamCertificateDO famCertificate = FamCertificateConvert.INSTANCE.convert(createReqVO);
        FamCertificateDO famCertificateDO = famCertificateMapper.selectOne(new LambdaQueryWrapperX<FamCertificateDO>()
                .eq(FamCertificateDO::getCreator, getLoginUserId()));
        if (famCertificateDO !=null ) {
            throw exception(CREATE_USER_CERTIFICATE_EXISTS);
        }
        famCertificateMapper.insert(famCertificate);
        // 返回
        return famCertificate.getId();
    }

    public IdcardUtil.Idcard verifyIdentityCard(String  card) {
        String idNum=card.toUpperCase();
        boolean validCard = IdcardUtil.isValidCard(idNum);
        if(!validCard) {
            throw exception(TREATMENT_USER_NOT_EXISTS_IDENTITYCARD);
        }
        return IdcardUtil.getIdcardInfo(idNum);
    }

    @Override
    public void updateFamCertificate(FamCertificateUpdateReqVO updateReqVO) {
        // 校验存在
        validateFamCertificateExists(updateReqVO.getId());
        // 更新
        FamCertificateDO updateObj = FamCertificateConvert.INSTANCE.convert(updateReqVO);
        famCertificateMapper.updateById(updateObj);
    }

    @Override
    public void deleteFamCertificate(Long id) {
        // 校验存在
        validateFamCertificateExists(id);
        // 删除
        famCertificateMapper.deleteById(id);
    }

    private void validateFamCertificateExists(Long id) {
        if (famCertificateMapper.selectById(id) == null) {
            throw exception(FAM_CERTIFICATE_NOT_EXISTS);
        }
    }

    @Override
    public FamCertificateDO getFamCertificate(Long id) {
        return famCertificateMapper.selectById(id);
    }

    @Override
    public List<FamCertificateDO> getFamCertificateList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return famCertificateMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<FamCertificateDO> getFamCertificatePage(FamCertificatePageReqVO pageReqVO) {
        return famCertificateMapper.selectPage(pageReqVO);
    }

    @Override
    public List<FamCertificateDO> getFamCertificateList(FamCertificateExportReqVO exportReqVO) {
        return famCertificateMapper.selectList(exportReqVO);
    }

    @Override
    public FamCertificateDO getLoginCertificate(Long loginUserId) {
        return famCertificateMapper.selectOne(new LambdaQueryWrapperX<FamCertificateDO>()
                .eq(FamCertificateDO::getCreator, getLoginUserId()));
    }

}
